package com.foreknow.web;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.foreknow.entity.Department;
import com.foreknow.entity.Expenseclass;
import com.foreknow.entity.Fmeditem;
import com.foreknow.service.impl.DepartmentServiceImpl;
import com.foreknow.service.impl.ExpenseclassServiceImpl;
import com.foreknow.service.impl.FmeditemServiceImpl;
import com.foreknow.utils.Constance;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/fmeditem")
public class FmeditemController {
    @Autowired
    private FmeditemServiceImpl fmeditemService;
    @Autowired
    private DepartmentServiceImpl departmentService;
    @Autowired
    private ExpenseclassServiceImpl expenseclassService;

    @RequestMapping(value = "/findFmeditemByPage", method = RequestMethod.POST)
    public String findUserByPage(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        System.out.println(para);
        int pageNum = (int) para.get("pagenum");
        int pageSize = (int) para.get("pagesize");
        String query = (String) para.get("query");
        if (query.equals("")) {
            query = null;
        }

        Fmeditem fmeditem = new Fmeditem();
        fmeditem.setQuery(query);

        PageHelper.startPage(pageNum, pageSize);
        List<Fmeditem> fmeditems = fmeditemService.queryAll(fmeditem);
        HashMap<String, Object> hs = new HashMap<>();
        if (fmeditems != null) {
            //PageInfo是PageHelper中内置的分页信息类
            PageInfo<Fmeditem> info = new PageInfo(fmeditems);
            System.out.println(info.getTotal());//可以获取到总记录数
            System.out.println(info.getList());//要显示的数据
            hs.put("data", info.getList());
            hs.put("meta", new Constance("msg", 200));
            hs.put("total", info.getTotal());
        } else {
            hs.put("meta", new Constance("获取列表失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/addFmeditem", method = RequestMethod.POST)
    public String addUser(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Fmeditem f = new Fmeditem();
        String itemCode = (String) para.get("itemcode");
        String itemName = (String) para.get("itemname");
        String price = (String) para.get("price");
        String format = (String) para.get("format");
        int deptname = (int) para.get("deptname");
        int expname = (int) para.get("expname");
        int RecordType = (int) para.get("recordtype");
        //获取系统时间
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH-mm-ss");
        Date date = new Date();
        String s = simpleDateFormat.format(date);
        System.out.println(date);
        f.setCreationdate(date);
        f.setItemcode(itemCode);
        f.setItemname(itemName);
        f.setPrice(BigDecimal.valueOf(Double.parseDouble(price)));
        f.setFormat(format);
        f.setDeptid(deptname);
        f.setExpclassid(expname);
        f.setRecordtype(RecordType);

        int isRight = fmeditemService.insert(f);
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("添加失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/deleteFmeditem", method = RequestMethod.DELETE)
    private String delete(@RequestParam Map<String, Object> para) throws JsonProcessingException {
        String id = (String) para.get("id");
        int isRight = fmeditemService.deleteById(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("删除失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectFmeditemOne", method = RequestMethod.GET)
    public String selectOne(@RequestParam Map<String, Object> para) throws JsonProcessingException {
        String id = (String) para.get("id");
        Fmeditem f = fmeditemService.queryOne(Integer.parseInt(id));
        HashMap<String, Object> hs = new HashMap<>();
        if (f != null) {
            hs.put("meta", new Constance("msg", 200));
            hs.put("data", f);
        } else {
            hs.put("meta", new Constance("查询失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/updateFmeditem", method = RequestMethod.POST)
    public String update(@RequestBody Map<String, Object> para) throws JsonProcessingException {
        Fmeditem f = new Fmeditem();
        String itemCode = (String) para.get("itemcode");
        String itemName = (String) para.get("itemname");
        int price = (int) para.get("price");
        String format = (String) para.get("format");
        int deptname = (int) para.get("deptname");
        int expname = (int) para.get("expname");
        int RecordType = (int) para.get("recordtype");
        //获取系统时间,设置信息更新时间
        f.setLastupdatedate(new Date());
        f.setId((int) para.get("id"));
        f.setItemcode(itemCode);
        f.setItemname(itemName);
        f.setPrice(BigDecimal.valueOf(price));
        f.setFormat(format);
        f.setDeptid(deptname);
        f.setExpclassid(expname);
        f.setRecordtype(RecordType);

        int isRight = fmeditemService.update(f);
        HashMap<String, Object> hs = new HashMap<>();
        if (isRight == 1) {
            hs.put("meta", new Constance("msg", 200));
        } else {
            hs.put("meta", new Constance("修改信息失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectDeptName", method = RequestMethod.GET)
    public String selectDeptName() throws JsonProcessingException {
        Department department = new Department();
        List<Department> d = departmentService.searchInfo(department);
        HashMap<String, Object> hs = new HashMap<>();
        if (d.size() > 0) {
            hs.put("meta", new Constance("msg", 200));
            hs.put("data", d);
        } else {
            hs.put("meta", new Constance("查询失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }

    @RequestMapping(value = "/selectExpName", method = RequestMethod.GET)
    public String selectExpName() throws JsonProcessingException {
        List<Expenseclass> e = expenseclassService.queryAll();
        HashMap<String, Object> hs = new HashMap<>();
        if (e != null) {
            hs.put("meta", new Constance("msg", 200));
            hs.put("data", e);
        } else {
            hs.put("meta", new Constance("查询失败", 201));
        }
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(hs);
    }
}